#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# What to test
# ------------
# - Delta plugin
#
# How to use this test
# --------------------
#
#
TestName="remote-fd-plugin-test"
. scripts/functions

scripts/cleanup
scripts/copy-test-confs

start_test

echo $REMOTE_FILE/save > $tmp/file-list

ssh $REMOTE_ADDR test -x /opt/bacula/bin/bacula-fd
if [ $? != 0 ]; then
    print_debug "ERROR: Need to install bacula on $REMOTE_ADDR"
    echo "ERROR: Need to install bacula on $REMOTE_ADDR"
    exit 1
fi

$bperl -e remote_stop
$bperl -e remote_init
$bperl -e remote_config
cfg=$conf/bacula-dir.conf
$bperl -e "add_attribute('$cfg', 'address', '$REMOTE_ADDR', 'Client')"
$bperl -e "add_attribute('$cfg', 'password', '$REMOTE_PASSWORD', 'Client')"
$bperl -e "add_attribute('$cfg', 'address', '$REMOTE_STORE_ADDR', 'Storage')"

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output
messages
@$out ${cwd}/tmp/log4.out
setdebug level=1 client
status client
@$out ${cwd}/tmp/log1.out
label storage=File volume=TestVolume001
run job=DeltaTest storage=File yes
wait
messages
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=$REMOTE_FILE/restore select all done storage=File
yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File
stop_bacula

check_two_logs

version=`$bin/bacula-dir -? 2>&1 | awk '/Version:/ { print $2 }'`
version_fd=`awk '/Version:/ { print $3 }' $tmp/log4.out`
 
if [ "$version" != "$version_fd" ]; then
    print_debug "ERROR: client version doesn't match $version ($version_fd)"
    estat=1
fi

grep delta $tmp/log4.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Can't find delta plugin in the status output"
    estat=2
fi

$bperl -e remote_diff
if [ $? != 0 ]; then
    dstat=1
fi

$bperl -e remote_stop

end_test
